package com.company.project.service.impl;

import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.dao.AdminLogMapper;
import com.company.project.dao.ShopLogMapper;
import com.company.project.dao.ShopLoginMapper;
import com.company.project.model.AdminLog;
import com.company.project.model.ShopLogin;
import com.company.project.model.request.QueryParams;
import com.company.project.model.response.PageList;
import com.company.project.service.AdminLogService;
import com.company.project.core.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;


/**
 * Created by CodeGenerator on 2020/04/21.
 */
@Service
@Transactional
public class AdminLogServiceImpl extends AbstractService<AdminLog> implements AdminLogService {
    @Resource
    private AdminLogMapper adminLogMapper;
    @Autowired
    private ShopLoginMapper shopLoginMapper;

    @Override
    public Result list(QueryParams queryParams) {
        int page = queryParams.getPage();
        int size = queryParams.getSize();
        if (queryParams.getTime() != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            queryParams.setTimeString(dateFormat.format(queryParams.getTime()));
        }
        queryParams.setStart((page - 1) * size);
        List<AdminLog> list = adminLogMapper.getLogList(queryParams);
        Integer total = adminLogMapper.getLogListTotal(queryParams);
        PageList<AdminLog> pageList = new PageList<>();
        pageList.setList(list);
        pageList.setTotal(total);
        return ResultGenerator.genSuccessResult(pageList);
    }

    @Override
    public Result shopList(QueryParams queryParams) {
        int page = queryParams.getPage();
        int size = queryParams.getSize();
        if (queryParams.getTime() != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            queryParams.setTimeString(dateFormat.format(queryParams.getTime()));
        }
        ShopLogin query = new ShopLogin();
        query.setShopId(queryParams.getId());
        ShopLogin shopLogin = shopLoginMapper.selectOne(query);
        queryParams.setName(shopLogin.getName());
        queryParams.setStart((page - 1) * size);
        List<AdminLog> list = adminLogMapper.getShopLogList(queryParams);
        Integer total = adminLogMapper.getShopLogListTotal(queryParams);
        PageList<AdminLog> pageList = new PageList<>();
        pageList.setList(list);
        pageList.setTotal(total);
        return ResultGenerator.genSuccessResult(pageList);
    }

    @Override
    public void deleteOne(String id) {
        AdminLog log = new AdminLog();
        log.setId(id);
        adminLogMapper.deleteByPrimaryKey(log);
    }
}
